//
//  HotColorSpeciesTwoViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/1.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "HotColorSpeciesTwoViewController.h"
#import "HelpViewController.h"
#import "ConfirmationBetViewController.h"
@interface HotColorSpeciesTwoViewController ()

@end

@implementation HotColorSpeciesTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    self.tabBarController.tabBar.hidden = YES;
    self.title = @"山东11选5";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_help"] style:UIBarButtonItemStylePlain target:self action:@selector(helpAction:)];
    
    PLFiveTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    PLFiveTableView.dataSource = self;
    PLFiveTableView.delegate = self;
    [PLFiveTableView registerNib:[UINib nibWithNibName:@"numberCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
    PLFiveTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:PLFiveTableView];
    
    [self addtoolBar];
    
    PLFiveTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
    UILabel *howToPlayLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 20, 30)];
    howToPlayLabel.text = @"玩\n法";
    howToPlayLabel.font = [UIFont systemFontOfSize:12];
    howToPlayLabel.numberOfLines = 0;
    howToPlayLabel.textAlignment = NSTextAlignmentCenter;
    [PLFiveTableView.tableHeaderView addSubview:howToPlayLabel];
    
    UILabel *howToPlay1Label = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, 60, 30)];
    howToPlay1Label.text = @"三码";
    howToPlay1Label.backgroundColor = [UIColor orangeColor];
    howToPlay1Label.textColor = [UIColor whiteColor];
    howToPlay1Label.textAlignment = NSTextAlignmentCenter;
    howToPlay1Label.font = [UIFont systemFontOfSize:14];
    [PLFiveTableView.tableHeaderView addSubview:howToPlay1Label];
    
    UILabel *howToPlay2Label = [[UILabel alloc]initWithFrame:CGRectMake(110, 10, 100, 30)];
    howToPlay2Label.text = @"前三直选复式";
    howToPlay2Label.textColor = [UIColor blackColor];
    howToPlay2Label.textAlignment = NSTextAlignmentCenter;
    howToPlay2Label.font = [UIFont systemFontOfSize:14];
    [PLFiveTableView.tableHeaderView addSubview:howToPlay2Label];

}

// 导航右侧按钮点击事件
- (void)helpAction:(UIButton *)sender {
    HelpViewController *hVC= [[HelpViewController alloc] init];
    [self.navigationController pushViewController:hVC animated:YES];
//    NSLog(@"1234");
}

- (void)addtoolBar
{
    _toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, UISCREEN_HEIGHT-49, UISCREEN_WIDTH, 49)];
    _toolbarView.backgroundColor = [UIColor whiteColor];
    //清空按钮
    UIButton *_clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_clearButton setImage:[UIImage imageNamed:@"icon_del"] forState:UIControlStateNormal];
    [_clearButton setImage:[UIImage imageNamed:@"icon_del"] forState:UIControlStateHighlighted];
    _clearButton.frame = CGRectMake(9, 3.5, 58, 34);
    [_clearButton addTarget:self action:@selector(pressedClearBut:) forControlEvents:UIControlEventTouchUpInside];
    [_toolbarView addSubview:_clearButton];
    UILabel *_clearButtonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 58, 34)];
    _clearButtonLabel.backgroundColor = [UIColor clearColor];
    _clearButtonLabel.font = [UIFont boldSystemFontOfSize:18];
    _clearButtonLabel.textColor = [UIColor whiteColor];
    _clearButtonLabel.textAlignment = NSTextAlignmentCenter;
    [_clearButton addSubview:_clearButtonLabel];
    
    //确定按钮
    UIButton *_sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton.frame = CGRectMake(255, 3.5, 58, 34);
    [_sureButton addTarget:self action:@selector(pressedSureBut:) forControlEvents:UIControlEventTouchUpInside];
    [_toolbarView addSubview:_sureButton];
    UILabel *_sureButtonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 58, 34)];
    _sureButtonLabel.text = @"确定";
    _sureButtonLabel.backgroundColor = [UIColor colorWithRed:90/225.0f green:188/225.0f blue:231/225.0f alpha:1];
    _sureButtonLabel.font = [UIFont boldSystemFontOfSize:14];
    _sureButtonLabel.textColor = [UIColor whiteColor];
    _sureButtonLabel.textAlignment = NSTextAlignmentCenter;
    [_sureButton addSubview:_sureButtonLabel];
    
    _numberNots = [[UILabel alloc] initWithFrame:CGRectMake(83, 0, 80, 43)];
    _numberNots.text = @"共0注";
    _numberNots.backgroundColor = [UIColor clearColor];
    _numberNots.font = [UIFont systemFontOfSize:16];
    _numberNots.textColor = [UIColor grayColor];
    _numberNots.textAlignment = NSTextAlignmentRight;
    [_toolbarView addSubview:_numberNots];
    
    
    _totalMoney = [[UILabel alloc] initWithFrame:CGRectMake(168, 0, 80, 43)];
    _totalMoney.text = @"0元";
    _totalMoney.backgroundColor = [UIColor clearColor];
    _totalMoney.font = [UIFont systemFontOfSize:16];
    _totalMoney.textColor = [UIColor grayColor];
    _totalMoney.textAlignment = NSTextAlignmentLeft;
    [_toolbarView addSubview:_totalMoney];
    
    
    
    [self.view addSubview:_toolbarView];
}
- (void)pressedSureBut: (UIButton *)sender
{
    NSLog(@"确定");
    _refreshData = NO;
    [PLFiveTableView reloadData];
    
    ConfirmationBetViewController *cBVC= [[ConfirmationBetViewController alloc] init];
    [self.navigationController pushViewController:cBVC animated:YES];

//    for (NSInteger i = 0 ; i<5 ; i++ ) {
//        NSMutableArray *tmpArr = [_makeSureSelectNumArray objectAtIndex:i];
//        NSLog(@"%d位：%@",i,tmpArr);
//        if ([tmpArr count] == 0) {
//            NSString *_weiZhiStr ;
//            switch (i) {
//                case 0:
//                    _weiZhiStr = [NSString stringWithFormat:@"请输入万位"];
//                    break;
//                case 1:
//                    _weiZhiStr = [NSString stringWithFormat:@"请输入千位"];
//                    break;
//                case 2:
//                    _weiZhiStr = [NSString stringWithFormat:@"请输入百位"];
//                    break;
//                case 3:
//                    _weiZhiStr = [NSString stringWithFormat:@"请输入十位"];
//                    break;
//                case 4:
//                    _weiZhiStr = [NSString stringWithFormat:@"请输入个位"];
//                    break;
//                default:
//                    break;
//            }
//            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:_weiZhiStr delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
//            [alertV show];
//            return;
//        }
//        
//    }
}

- (void)pressedClearBut: (UIButton *)sender
{
    NSLog(@"清空");
    _refreshData = YES;
    [_shakeNumber removeAllObjects];
    [PLFiveTableView reloadData];
    _numberNots.text = @"共0注";
    _totalMoney.text = @"0元";
    
}


- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 30;
    }
    else
    {
        return 90;
    }
}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        NSString *  CellId = @"firstCell";
        shakedViewCell *cell = [[shakedViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId withTitle:@"距本期截止：00：00：25"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
        
        static NSString *cellIdent = @"numberCell";
        numberCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
        if (cell == nil) {
            cell = [[numberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
            
        }
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
}

#pragma mark --
#pragma mark -- NumberCellDelegate
//当前界面在选号时不可滑动，cell来控制
- (void)tableViewScrll:(BOOL)stop
{
    if (stop) {
        PLFiveTableView.scrollEnabled = YES;
        NSLog(@"*****可滚动*****");
        
    }else
    {
        PLFiveTableView.scrollEnabled = NO;
        NSLog(@"*****不可滚动*****");
        
    }
    
}

//选中或取消号码后产生的注数和钱数

- (void)getTotalNotsAndMoney
{
    NSInteger totalNumber = 1;
    [PLFiveTableView reloadData];
    NSLog(@"数组%d",[_makeSureSelectNumArray count]);
    for (NSInteger i = 0; i<5; i++) {
        NSMutableArray *tmpArray = [_makeSureSelectNumArray objectAtIndex:i];
        totalNumber *= [tmpArray count];
    }
    _numberNots.text = [NSString stringWithFormat:@"共%d注",totalNumber];
    _totalMoney.text = [NSString stringWithFormat:@"%d元",totalNumber*2];
    NSLog(@"%d",totalNumber);
}

#pragma mark --
#pragma mark -- 摇一摇机选
- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
    NSLog(@"检测到摇动");
    [_shakeNumber removeAllObjects];
    for (int i = 0; i<5; i++) {
        int j = arc4random()%10;
        NSString *selectNumStr = [NSString stringWithFormat:@"%d",j];
        [_shakeNumber addObject:selectNumStr];
        NSLog(@"selectNumStr:%@",selectNumStr);
    }
    NSLog(@"*_shakeNumber:%@",_shakeNumber);
    
    _refreshData = YES;
    [PLFiveTableView reloadData];
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇动取消");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇动结束");
    if (event.subtype == UIEventSubtypeMotionShake) {
        NSLog(@"UIEventSubtypeMotionShake---摇动结束");
        
        _numberNots.text = @"共1注";
        _totalMoney.text = @"2元";
        _refreshData = NO;
    }
}


@end
